package com.akuhome.resource.service;

import com.akuhome.resource.DO.need.AccNeed;
import com.akuhome.resource.DO.need.ClassroomNeed;
import com.akuhome.resource.DO.need.FoodNeed;
import com.akuhome.resource.dto.Form;
import com.akuhome.resource.entity.Project;
import com.akuhome.resource.enums.NeedStatus;
import com.akuhome.resource.enums.NeedType;
import com.akuhome.resource.enums.ProjectStatus;
import com.akuhome.resource.service.entity.ProjectService;
import com.akuhome.resource.service.need.AccNeedService;
import com.akuhome.resource.service.need.ClassroomNeedService;
import com.akuhome.resource.service.need.FoodNeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FormService {
    @Autowired
    ProjectService projectService;
    @Autowired
    AccNeedService accNeedService;
    @Autowired
    FoodNeedService foodNeedService;
    @Autowired
    ClassroomNeedService classroomNeedService;
    public Form getAllById(Integer id){
        return getById(id,true,true,true,true);
    }

    public Form getAccById(Integer id){
        return getById(id,true,true,false,false);
    }

    public Form getClassroomById(Integer id){
        return getById(id,true,false,false,true);
    }

    public Form getFoodById(Integer id){
        return getById(id,true,false,true,false);
    }

    public Form getAllByUId(String uid){
        return getByUId(uid,true,true,true,true);
    }

    public Form getAccByUId(String uid){
        return getByUId(uid,true,true,false,false);
    }

    public Form getClassroomByUId(String uid){
        return getByUId(uid,true,false,false,true);
    }

    public Form getFoodByUId(String uid){
        return getByUId(uid,true,false,true,false);
    }

    private Form getById(Integer id,Boolean project,Boolean acc,Boolean food,Boolean classroom){
        Form form = new Form();
        Project p = projectService.findById(id);
        AccNeed accNeed;
        FoodNeed foodNeed;
        ClassroomNeed classroomNeed;
        if(project){
            form.setProject(p);
        }

        if(acc) {
            accNeed = accNeedService.findNeedByProjectUid(p.getUid());
            form.setAccNeed(accNeed);
        }

        if(food){
            foodNeed = foodNeedService.findNeedByProjectUid(p.getUid());
            form.setFoodNeed(foodNeed);
        }

        if (classroom){
            classroomNeed = classroomNeedService.findNeedByProjectUid(p.getUid());
            form.setClassroomNeed(classroomNeed);
        }

        return form;
    }

    private Form getByUId(String uid,Boolean project,Boolean acc,Boolean food,Boolean classroom){
        Form form = new Form();
        Project p = projectService.findByUId(uid);
        AccNeed accNeed;
        FoodNeed foodNeed;
        ClassroomNeed classroomNeed;
        if(project){
            form.setProject(p);
        }

        if(acc) {
            accNeed = accNeedService.findNeedByProjectUid(uid);
            form.setAccNeed(accNeed);
        }

        if(food){
            foodNeed = foodNeedService.findNeedByProjectUid(uid);
            form.setFoodNeed(foodNeed);
        }

        if (classroom){
            classroomNeed = classroomNeedService.findNeedByProjectUid(uid);
            form.setClassroomNeed(classroomNeed);
        }

        return form;
    }

    public void updateForm(Form form, List<NeedType> type){
        String uid = form.getProject().getUid();
        type.forEach((value)->{
            switch (value){
                case ACC_NEED:
                    accNeedService.updateNeed(form.getAccNeed());
                    accNeedService.updateNeedStatusByProjectUid(uid, NeedStatus.IN_APPROVAL);
                    break;
                case CLASSROOM_NEED:
                    classroomNeedService.updateNeed(form.getClassroomNeed());
                    classroomNeedService.updateNeedStatusByProjectUid(uid,NeedStatus.IN_APPROVAL);
                    break;
                case FOOD_NEED:
                    foodNeedService.updateNeed(form.getFoodNeed());
                    foodNeedService.updateNeedStatusByProjectUid(uid,NeedStatus.IN_APPROVAL);
                    break;
            }
        });
        projectService.updateProjectStatus(form.getProject().getUid(), ProjectStatus.IN_APPROVAL);
    }
}
